package com.itheima06;

import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;

/**
 * @author Xiao
 * @date 2021/7/26
 */
public class TeacherTest01 {
    /**
     * 需求：
     *      自定义Teacher类，属性为姓名和年龄。请按照年龄排序，
     *      如果年龄一样，则按照姓名进行排序（姓名都是用英文字母表示）。
     */
    public static void main(String[] args) {
        Teacher t1 = new Teacher("Lisa1", 24);
        Teacher t2 = new Teacher("Lisa1", 24);
        Teacher t3 = new Teacher("Lisa2", 23);
        Teacher t4 = new Teacher("Lisa2", 24);
        Teacher t5 = new Teacher("Lisa3", 22);
        TreeSet<Teacher> ts = new TreeSet<Teacher>(new Comparator<Teacher>() {
            @Override
            public int compare(Teacher o1, Teacher o2) {
//                return compareTo(o1, o2);
                int result = o1.getAge() - o2.getAge();
                return result == 0 ? o1.getName().compareTo(o2.getName()) : result;
            }
        });
        Collections.addAll(ts, t1, t2, t3, t4, t5);
        for (Teacher t : ts) {
            System.out.println(t);
        }
    }

    private static int compareTo(Teacher o1, Teacher o2) {
        if (o1.getAge() != o2.getAge()) {
            return o1.getAge() - o2.getAge();
        }
        return o1.getName().compareTo(o2.getName());
    }
}
